package com.panopset;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.panopset.compat.Strings;

/**
 * Keeps tally of Integer results.
 */
public final class Tally {

    @Override
    public String toString() {
        StringWriter sw = new StringWriter();
        for (Entry<Integer, Integer> entry : counts.entrySet()) {
            sw.append("" + entry.getKey() + " count: " + entry.getValue());
            sw.append(Strings.getEol());
        }
        return sw.toString();
    }

    /**
     * @param r
     *            Add a result to the tally.
     */
    public void tally(final int r) {
        Integer count = counts.get(r);
        if (count == null) {
            counts.put(r, 1);
        } else {
            count++;
            counts.put(r, count);
        }
    }

    /**
     * Counts map.
     */
    private final Map<Integer, Integer> counts

    = new HashMap<Integer, Integer>();

    /**
     *
     * @return Counts.
     */
    public Map<Integer, Integer> getCounts() {
        return counts;
    }
}
